package com.dodayum;

import org.quartz.*;
import org.springframework.stereotype.Service;

@PersistJobDataAfterExecution // 共享数据
@DisallowConcurrentExecution // 并发问题
@Service
public class MyJob implements Job {
    @Override
    public void execute(JobExecutionContext context) {
        // context表示定时任务执行的环境
        JobDetail jobDetail = context.getJobDetail();
        JobDataMap jobDataMap = jobDetail.getJobDataMap(); // 共享数据
        String countStr = jobDataMap.get("count").toString();
        Integer count = Integer.parseInt(countStr);
        // 任务详细信息
        System.out.println("***************************************");
        System.out.println("第\t" + count + "\t次执行");
        System.out.println("任务名字：" + jobDetail.getKey().getName());
        System.out.println("任务分组名字：" + jobDetail.getKey().getGroup());
        System.out.println("任务类名字：" + jobDetail.getJobClass().getName());
        System.out.println("本次执行时间：" + context.getFireTime());
        System.out.println("下次执行时间：" + context.getNextFireTime());

        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println("执行完成");
        count += 1;
        jobDataMap.put("count", count);
    }
}
